residence <-
  data_1 %>%
  filter(abs(forcing_skd) < 5) %>%
  group_by(quintile_relg_diff_residence) %>%
  do(tidy(lm_robust(religious_index_skd_bw_1 ~ fail_skd, data = .))) %>%
  filter(term == "fail_skd", !is.na(quintile_relg_diff_residence)) %>%
  mutate(type = "By Place of Residence") %>%
  rename(quintile = quintile_relg_diff_residence) %>%
  mutate(quintile = factor(quintile, levels = c("[0, 3.7)","[3.7, 6.9)",  "[6.9,10.9)", "[10.9,17.5)", "[17.5,66.1)"))) %>%
  arrange(quintile) %>%
  ungroup() %>%
  mutate(quint_lab = seq_along(estimate)) %>%
  dplyr::select(quint_lab, estimate_res = estimate, std.error_res = std.error)

job_loc <-
  data_1 %>%
  filter(abs(forcing_skd) < 5) %>%
  group_by(quintile_relg_diff_job_loc) %>%
  do(tidy(lm_robust(religious_index_skd_bw_1 ~ fail_skd, data = .))) %>%
  filter(term == "fail_skd", !is.na(quintile_relg_diff_job_loc)) %>%
  mutate(type = "By Job Location") %>%
  rename(quintile = quintile_relg_diff_job_loc) %>%
  mutate(quintile = factor(quintile, levels = c("[0, 3.8)", "[3.8, 7.4)", "[7.4,10.7)", "[10.7,15.2)", "[15.2,66.8)"))) %>%
  arrange(quintile) %>%
  ungroup() %>%
  mutate(quint_lab = seq_along(estimate)) %>%
  dplyr::select(quint_lab, estimate_loc = estimate, std.error_loc = std.error)

left_join(residence, job_loc) %>%
  mutate_at(vars(estimate_res:std.error_loc), funs(round(., 3))) %>%
  kbl(., 
      format = "latex",
      caption = "Tabular Presentation of Figure 4", 
      booktabs = T,
      escape = F,
      linesep = "",
      align = "lcccc",
      col.names = c("Quintile", "Estimate", "SE", "Estimate", "SE")) %>%
  kable_styling() %>% 
  add_header_above(c(" " = 1, "Difference by Place of Residence" = 2, "Difference by Job Location" = 2)) %>%
  #column_spec(column = 1, width = "2in") %>%
  as.character() %>%
  str_replace(., "\\\\begin\\{table\\}", "\\\\begin{table}[!htbp]") %>%
  cat(., file = "./_4_outputs/tables/table_a18.tex")


  